﻿using System.Diagnostics;
using System.Windows;
using System.Windows.Navigation;

using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

using SSTU.Data;
using SSTU.Management.Dependency;
using SSTU.Management.Module;
using System;

namespace SSTU
{
	public partial class App : Application
	{
		/// <summary>
		/// Provides easy access to the root frame of the Phone Application.
		/// </summary>
		/// <returns>The root frame of the Phone Application.</returns>
		public PhoneApplicationFrame RootFrame { get; private set; }

		/// <summary>
		/// Constructor for the Application object.
		/// </summary>
		public App()
		{
			// Global handler for uncaught exceptions. 
			UnhandledException += OnApplicationUnhandledException;
			// Standard Silverlight initialization
			InitializeComponent();
			// Phone-specific initialization
			InitializePhoneApplication();
			// Dependency and other initialization.
			SstuDataContext.InitializeAsync();
			var sstuModule = new SstuModule(RootFrame);
			DependencyContainer.Current.Initialize(sstuModule);
		}

		/// <summary>
		/// Code to execute when the application is launching (eg, from Start).
		/// This code will not execute when the application is reactivated.
		/// </summary>
		private void OnApplicationLaunching(object sender, LaunchingEventArgs e)
		{
		}

		/// <summary>
		/// Code to execute when the application is activated (brought to foreground).
		/// This code will not execute when the application is first launched.
		/// </summary>
		private void OnApplicationActivated(object sender, ActivatedEventArgs e)
		{
		}

		/// <summary>
		/// Code to execute when the application is deactivated (sent to background).
		/// This code will not execute when the application is closing.
		/// </summary>
		private void OnApplicationDeactivated(object sender, DeactivatedEventArgs e)
		{
		}

		/// <summary>
		/// Code to execute when the application is closing (eg, user hit Back).
		/// This code will not execute when the application is deactivated.
		/// </summary>
		private void OnApplicationClosing(object sender, ClosingEventArgs e)
		{
		}

		/// <summary>
		/// Code to execute if a navigation fails.
		/// </summary>
		private void OnRootFrameNavigationFailed(object sender, NavigationFailedEventArgs e)
		{
			if (System.Diagnostics.Debugger.IsAttached)
			{
				// A navigation has failed; break into the debugger.
				Debugger.Break();
			}
			e.Handled = true;
			RootFrame.Dispatcher.BeginInvoke(() => MessageBox.Show("Приносим свои извинения, произошла неизвестная ошибка."));
		}

		/// <summary>
		/// Called when in application occurs unhandled exception.
		/// </summary>
		private void OnApplicationUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
		{
			if (System.Diagnostics.Debugger.IsAttached)
			{
				// An unhandled exception has occurred; break into the debugger
				Debugger.Break();
			}
			e.Handled = true;
			RootFrame.Dispatcher.BeginInvoke(() => MessageBox.Show("Приносим свои извинения, произошла неизвестная ошибка."));
		}

		#region Phone application initialization

		// Avoid double-initialization
		private bool phoneApplicationInitialized = false;

		// Do not add any additional code to this method
		private void InitializePhoneApplication()
		{
			if (phoneApplicationInitialized)
			{
				return;
			}
			// Create the frame but don't set it as RootVisual yet; this allows the splash
			// screen to remain active until the application is ready to render.
			RootFrame = new PhoneApplicationFrame();
			RootFrame.Navigated += CompleteInitializePhoneApplication;
			// Handle navigation failures.
			RootFrame.NavigationFailed += OnRootFrameNavigationFailed;
			// Ensure we don't initialize again.
			phoneApplicationInitialized = true;
		}

		// Do not add any additional code to this method
		private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
		{
			// Set the root visual to allow the application to render
			if (RootVisual != RootFrame)
			{
				RootVisual = RootFrame;
			}
			// Remove this handler since it is no longer needed
			RootFrame.Navigated -= CompleteInitializePhoneApplication;
		}

		#endregion
	}
}